<?php
namespace app\patrol\logic;

class CommunityPatrolLogic extends PatrolLogic {

    /**
     * 检查社区巡店分部规则
     * @param $cp_branchid
     * @param $branchid
     * @return mixed
     */
    public function check_cp_branch_rule($cp_branchid,$branchid){
        //获取社区巡店分部信息
        $cp = new \app\common\controller\Cp();
        $cp_branch_info = $cp->get_cp_branch_info($branchid);
        if(!$cp_branch_info){
            return $this->get_response(0,'系统异常，请联系管理员');
        }
        if($cp_branchid != $cp_branch_info['id']){
            return $this->get_response(0,'该时间端已过，请重新进入');
        }

        $data['cp_branch_info'] = $cp_branch_info;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 更新社区巡店楼层
     * @param $openid
     * @param $cp_type
     * @param $crm_detail
     * @return mixed
     */
    public function save_cp_floor($openid,$cp_type,$crm_detail){
        $maps = [];
        $maps[] = ['cp_type','=',$cp_type];
        $maps[] = ['cp_floorid','=',$crm_detail['cp_floorid']];
        $maps[] = ['status','=',0];
        $detail = model('cp_region_modular')->get_detail($maps,'id');
        if(empty($detail)){
            $cf_data = [];
            $cf_data['end_time'] = date('Y-m-d H:i:s');
            $cf_data['status'] = 1;

            $maps = [];
            $maps[] = ['id','=',$crm_detail['cp_floorid']];
            $r = model('cp_floor')->force()->save($cf_data,$maps);
            if($r !== false){
                $maps = [];
                $maps[] = ['cp_type','=',$cp_type];
                $maps[] = ['cp_floorid','=',$crm_detail['cp_floorid']];
                $crmids = model('cp_region_modular')->get_column($maps,'id');

                $m = new \app\common\controller\Matter();
                $c = new \app\common\controller\Copy();
                foreach($crmids as $k => $v){
                    //生成事项（打开）
                    $r = $m->save_matter_open($openid,$v,1);
                    //生成抄送（已抄送）
                    $r = $c->save_copy_open($openid,$v,1);
                }
                return $this->get_response(1,'OK');
            }
            return $this->get_response(0,'更新社区巡店楼层失败');
        }
        return $this->get_response(1,'OK');
    }

    /**
     * 更新社区巡店分部
     * @param $cp_type
     * @param $crm_detail
     * @return mixed
     */
    public function save_cp_branch($cp_type,$crm_detail){
        //查找已巡店楼层
        $maps = [];
        $maps[] = ['cp_type','=',$cp_type];
        $maps[] = ['cp_branchid','=',$crm_detail['cp_branchid']];
        $maps[] = ['status','=',1];
        $cp_floorids = model('cp_floor')->get_column($maps,'floorid');
        $cp_floorids = array_unique($cp_floorids);

        //查找原本应该巡店楼层
        $maps = [];
        $maps[] = ['branchid','=',$crm_detail['branchid']];
        $floorids = model('floor')->get_column($maps,'id');

        //求两个数组的差集
        $diff_arr = array_diff($floorids,$cp_floorids);
        if(empty($diff_arr)){
            $cb_data = [];
            $cb_data['end_time'] = date('Y-m-d H:i:s');
            $cb_data['status'] = 1;

            $maps = [];
            $maps[] = ['id','=',$crm_detail['cp_branchid']];
            $r = model('cp_branch')->force()->save($cb_data,$maps);
            if($r !== false){
                //更新社区巡店积分
                $si = new \app\common\controller\SaveIntegral();
                $r = $si->save_cp_integral($crm_detail['cp_branchid']);

                return $this->get_response(1,'OK');
            }
            return $this->get_response(0,'更新社区巡店分部失败');
        }
        return $this->get_response(1,'OK');
    }
}